import Vue from "vue";
//添加token
const addToken = (value) => {
	uni.setStorageSync("sessionId", value);
};

//获取token
const getToken = () => {
	return uni.getStorageSync("sessionId");
};
//清理
const removeToken = () => {
	uni.removeStorageSync("sessionId");
};

//添加用户信息
const addUserInfo = (value) => {
	uni.setStorageSync("userInfo", value);
};

//添加资产列表
const addAssets = (value) => {
	uni.setStorageSync("Assets", value);
};
//获取资产列表
const getAssets = (value) => {
	uni.setStorageSync("Assets", value);
};

//获取用户信息
const getUserInfo = () => {
	return uni.getStorageSync("userInfo");
};

//清理
const removeUserInfo = () => {
	uni.removeStorageSync("userInfo");
};

//添加id
const addObject = (value) => {
	uni.setStorageSync("Object", value);
};

//获取id
const getObject = () => {
	return uni.getStorageSync("Object");
};

//车牌号校验包含新能源
const carPhoneValid = (value) => {
	var xreg =
		/^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1})$/;
	var xxreg =
		/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DABCEFGHJK]$)|([DABCEFGHJK][A-HJ-NP-Z0-9][0-9]{4}$))/; // 2021年新能源车牌不止有DF
	if (value.length == 7) {
		return xreg.test(value);
	} else if (value.length == 8) {
		return xxreg.test(value);
	} else {
		return false;
	}
};

//车架号
const frameNumber = (value) => {
	const Number = /^[A-HJ-NPR-Z\d]{8}[X\d][A-HJ-NPR-Z\d]{3}\d{5}$/;
	if (!Number.test(value)) {
		return false;
	} else {
		return true;
	}
};

// 纳税人识别号

const identiFication = (value) => {
	const num = /[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}/;
	if (!num.test(value)) {
		return false;
	} else {
		return true;
	}
};
// 跳转路由-判断有无token
// const navigateTo = () => {
//   if (getToken()) {
//     uni.navigateTo({
//       url: "/pages/application/index",
//     });
//   } else {
//     uni.navigateTo({
//       url: "/pages/register/index",
//     });
//   }
// };

const Login = () => {
	return new Promise((resolve, reject) => {
		// if (getToken() && getUserInfo()) {
		//   resolve();
		// } else {
		removeToken();
		removeUserInfo();
		const accountInfo = uni.getAccountInfoSync();
		console.log(accountInfo.miniProgram.appId, 99999); // 小程序 appId

		//登录
		wx.login({

			success: (r) => {


				Vue.prototype.$u.api
					.GetTokenByCode({
						code: r.code,
						appid: accountInfo.miniProgram.appId
					})

					.then((res) => {
						console.log(res, '8989')
						if (res) {

							var token =
								"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjhmNWYxZDg0LWJjZmItNDg2Zi1hNDRkLTBkMjU4OTgyN2EwNCIsImV4cCI6MTY1MDI1NTAyMSwiaXNzIjoiV01QIn0.1AmApEoePUQD9UptMn8ztHyjAZ1x73sA6mqCDPHAhyQ"

							addToken(token)

							// console.log(res, '78878')
							// addToken(res.msg);
							// Vue.prototype.$u.api.GetUserByCode({
							// 	appid: "1000012"
							// }).then((res) => {
							// 	addUserInfo(res.msg);
							// 	Vue.prototype.$store.commit("userInfo", res.msg);
							// });
							resolve();
						} else {
							var token =
								"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjhmNWYxZDg0LWJjZmItNDg2Zi1hNDRkLTBkMjU4OTgyN2EwNCIsImV4cCI6MTY1MDI1NTAyMSwiaXNzIjoiV01QIn0.1AmApEoePUQD9UptMn8ztHyjAZ1x73sA6mqCDPHAhyQ"

							addToken(token)
							reject();
						}
					});
			},
		});
		// }
	});
};

module.exports = {
	addToken,
	getToken,
	addUserInfo,
	getUserInfo,
	Login,
	removeToken,
	removeUserInfo,
	carPhoneValid,
	frameNumber,
	identiFication,
	addAssets,
	getAssets,
};
